# coding = utf-8
import requests
from bs4 import BeautifulSoup


def getHTMLText(url, coding='utf-8'):
    try:
       headers = {   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0 Win64 x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36'}
       r = requests.get(url,headers = headers)
       r.raise_for_status()
       r.encoding = coding
       return r.text
    except:
       return ""


def getListInfo(html, lists):
    soup = BeautifulSoup(html,'html.parser')
    pics = soup.find_all('div','pic')
    for pic in pics:
        imgUrl = pic.find('img').attrs['src']
        title = pic.find('img').attrs['alt']
        lists.append([title,imgUrl])
    pass

def saveInfo(lists):
    with open('douban.txt','w',encoding='utf-8') as f:
        for i in lists:
            f.write('(\''+i[0]+',\''+i[1]+'\')\n')
            pass
    pass


def main():
    startUrl = 'https://movie.douban.com/top250?start='
    lists =[]
    for i in range(10):
        html = getHTMLText(startUrl+str(i*25))
        getListInfo(html,lists)
    saveInfo(lists)

if __name__ == "__main__":
    main()
    pass
